package com.yzdg.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yzdg.auth.service.AuthUserService;
import com.yzdg.common.entitys.BaseResponse;
import com.yzdg.common.utils.ResultUtils;
import com.yzdg.model.auth.dtos.AuthUserDTO;
import com.yzdg.model.auth.vos.AuthUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthUserController {

    @Resource
    private AuthUserService authUserService;

    /**
     * 测试接口
     */
    @GetMapping("/test")
    public BaseResponse<Boolean> test(){
        return ResultUtils.success(true);
    }

    /**
     * 登录/注册
     */
    @PostMapping("/login")
    public BaseResponse<AuthUserVO> login(@RequestBody @Valid AuthUserDTO authUserDTO) {
        log.info("登录/注册参数:{}", authUserDTO);
        AuthUserVO authUserVO = authUserService.login(authUserDTO);
        return ResultUtils.success(authUserVO);
    }

    /**
     * 验证是否登录
     */
    @PostMapping("/isLogin")
    public BaseResponse<Boolean> isLogin() {
        boolean login = StpUtil.isLogin();
        return ResultUtils.success(login);
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> logout() {
        StpUtil.logout();
        return ResultUtils.success(true);
    }
}
